#include "stdafx.h"

const int cHostPort = 58213;

const unsigned cNetChat = 1;

bool terminateApp = false;

DWORD _stdcall ThreadInput(void* arg)
{
	while (!terminateApp)
	{
		std::string text;
		std::cin >> text;
		//net::NetChat::I()->PushLine(text);

		int m;
		std::stringstream sstream(text);
		sstream >> m;
		net::NetChat::I()->testInt = m;

		if (text == "exit")
			terminateApp = true;
	}

	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	lsl::appLog.Clear();

	LSL_LOG("server init");

	net::INetService& netService = net::GetNetService();
	
	netService.modelClasses().Add<net::NetChat>(cNetChat);
	netService.syncRate(70);

	lsl::StringVec addresses;
	netService.GetAdapterAddresses(addresses);

	for (unsigned i = 0; i < addresses.size(); ++i)
		LSL_LOG(lsl::StrFmt("address %d = %s", i, addresses[i].c_str()));

	LSL_LOG("server starting...");

	netService.Initializate();
	netService.StartServer(cHostPort);

	LSL_LOG("server start");

	netService.player()->MakeModel<net::NetChat>();
	CreateThread(0, 0, &ThreadInput, NULL, 0, 0);

	__int64 cpuFreq;
	QueryPerformanceFrequency((LARGE_INTEGER*)&cpuFreq);
	__int64 tick;
	QueryPerformanceCounter((LARGE_INTEGER*)&tick);

	while (!terminateApp)
	{
		Sleep(16);

		__int64 newTick;
		QueryPerformanceCounter((LARGE_INTEGER*)&newTick);
		unsigned time = static_cast<unsigned>((newTick - tick) * 1000 / cpuFreq);

		netService.Process(time);

		//if (net::NetChat::I())
		//	net::NetChat::I()->testInt = RandomRange(0, 1000);
	}

	netService.Finalizate();

	return 0;
}

